package th.kmutt.se.is.model;

import java.util.ArrayList;
import java.util.List;

import th.kmutt.se.is.fact.Database;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils.InsertHelper;
import android.util.Log;

public class Room {
	
	
	private int id;
	private String name;
	private int floor;
	private double price;
	private double depositPrice;
	private int waterMeter;
	private int electMeter;
	private int status;
	private Tenant tenant[];
	private Charges charge[];

	private Context context;
	private Database db;
	
	final String[] cols = new String[] {
			Database.KEY_ID, Database.KEY_NAME, 
			Database.KEY_FL, Database.KEY_PRICE, 
			Database.KEY_DEP_PRICE, Database.KEY_METER_W, 
			Database.KEY_METER_E, Database.KEY_STATUS};
	
	public Room(Context context){
		this.context = context;
		db = new Database(context);
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getFloor() {
		return floor;
	}
	public void setFloor(int floor) {
		this.floor = floor;
	}
	public double getPrice() {
		return price;
	}
	public double getDepositPrice() {
		return depositPrice;
	}

	public void setDepositPrice(double depositPrice) {
		this.depositPrice = depositPrice;
	}

	public int getWaterMeter() {
		return waterMeter;
	}

	public void setWaterMeter(int waterMeter) {
		this.waterMeter = waterMeter;
	}

	public int getElectMeter() {
		return electMeter;
	}

	public void setElectMeter(int electMeter) {
		this.electMeter = electMeter;
	}

	public void setPrice(double price) {
		this.price = price;
	}
	public int getStatus() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	public Tenant[] getTenant() {
		return tenant;
	}
	public void setTenant(Tenant[] tenant) {
		this.tenant = tenant;
	}
	public Charges[] getCharge() {
		return charge;
	}
	
	public void setCharge(Charges[] charge) {
		this.charge = charge;
	}
	
	public boolean create(){
		db.openToWrite();
		InsertHelper ih = new InsertHelper(db.sqLiteDatabase, Database.TB_ROOM);
		final int colName = ih.getColumnIndex(cols[1]);
		final int colFloor = ih.getColumnIndex(cols[2]);
		final int colPrice = ih.getColumnIndex(cols[3]);
		final int colDepPrice = ih.getColumnIndex(cols[4]);
		final int colWMeter = ih.getColumnIndex(cols[5]);
		final int colEMeter = ih.getColumnIndex(cols[6]);
		final int colStatus = ih.getColumnIndex(cols[7]);

		try {
			ih.prepareForInsert();
			ih.bind(colName, getName());
			ih.bind(colFloor, getFloor());
			ih.bind(colPrice, getPrice());
			ih.bind(colDepPrice, getDepositPrice());
			ih.bind(colWMeter, getWaterMeter());
			ih.bind(colEMeter, getElectMeter());
			ih.bind(colStatus, getStatus());
			ih.execute();
		} catch (Exception e) {
			Log.w("Exception on create room ", e.toString());
			return false;
		}finally{			
			ih.close();
			db.close();
		}
		return true;
	}

	public Room getRoomById(int roomId){
		db.openToRead();
		Cursor cursor = db.sqLiteDatabase.query(Database.TB_ROOM, cols, cols[0]+"="+roomId, null, null, null, null);
		if(cursor!=null){
			if(cursor.getCount()>0){
				cursor.moveToFirst();
				while(!cursor.isAfterLast()){	
					setId(cursor.getInt(cursor.getColumnIndex(cols[0])));
					setName(cursor.getString(cursor.getColumnIndex(cols[1])));
					setFloor(cursor.getInt(cursor.getColumnIndex(cols[2])));
					setPrice(cursor.getDouble(cursor.getColumnIndex(cols[3])));
					setDepositPrice(cursor.getDouble(cursor.getColumnIndex(cols[4])));
					setWaterMeter(cursor.getInt(cursor.getColumnIndex(cols[5])));
					setElectMeter(cursor.getInt(cursor.getColumnIndex(cols[6])));
					setStatus(cursor.getInt(cursor.getColumnIndex(cols[7])));
					
					cursor.moveToNext();
				}
				cursor.close();
				db.close();
			}else{
				setId(0);
				cursor.close();
				db.close();
				return null;
			}
		}
		return this;
	}
	
	public Room[] getRoomList(int status){
		List<Room> roomList = new ArrayList<Room>();
		
		db.openToRead();
		Cursor cursor;
		if(status==0){
			cursor = db.sqLiteDatabase.query(Database.TB_ROOM, cols, null, null, null, null, null);
		}else{
			cursor = db.sqLiteDatabase.query(Database.TB_ROOM, cols, cols[7]+"="+status, null, null, null, null);
		}
		
		if(cursor!=null){
			if(cursor.getCount()>0){
				cursor.moveToFirst();
				while(!cursor.isAfterLast()){
					Room r = new Room(context);

					r.setId(cursor.getInt(cursor.getColumnIndex(cols[0])));
					r.setName(cursor.getString(cursor.getColumnIndex(cols[1])));
					r.setFloor(cursor.getInt(cursor.getColumnIndex(cols[2])));
					r.setPrice(cursor.getDouble(cursor.getColumnIndex(cols[3])));
					r.setDepositPrice(cursor.getDouble(cursor.getColumnIndex(cols[4])));
					r.setWaterMeter(cursor.getInt(cursor.getColumnIndex(cols[5])));
					r.setElectMeter(cursor.getInt(cursor.getColumnIndex(cols[6])));
					r.setStatus(cursor.getInt(cursor.getColumnIndex(cols[7])));
					
					roomList.add(r);
					cursor.moveToNext();
				}
				cursor.close();
				db.close();
			}else{
				cursor.close();
				db.close();
				return null;
			}
		}
		return roomList.toArray(new Room[roomList.size()]);
	}
}
